﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace HSS.Interlink.Client.SL5.TestApp {
	public partial class NoUIMainPage : Page {

		UploadClient uploader;

		public NoUIMainPage() {
			InitializeComponent();

			string url = Application.Current.Host.Source.GetComponents(UriComponents.Scheme |
																	   UriComponents.HostAndPort,
																	   UriFormat.Unescaped);
			this.uploader = new UploadClient();
			this.uploader.Initialize(url + "/FileUpload.ashx", "UploadHandler", false);
			this.uploader.UploadCompleted += uploader_UploadCompleted;

		}

		void uploader_UploadCompleted(object sender, UploadFileCompletedEventArgs e) {
			if (e.Error != null) {
				MessageBox.Show(e.Error.ToString());
				return;
			}
			if (e.Cancelled) {
				MessageBox.Show("Canceled.");
				return;
			}
			MessageBox.Show(e.Result);
		}

		// Executes when the user navigates to this page.
		protected override void OnNavigatedTo(NavigationEventArgs e) {
		}

		private void btnTest_Click(object sender, RoutedEventArgs e) {
			//AttachmentData data = (AttachmentData)op.UserState;
			//string metadata = JsonConvert.SerializeObject(new AttachmentModel {
			//	EntityId = ((ConsultationAppointmentModel)CurrentAppointmentView.CurrentItem).ConsultationAppointmentId,
			//	FileName = data.docName,
			//	FileDescription = "Descripción de prueba",
			//	Id = Guid.NewGuid(),
			//	Kind = AttachmentKind.ConsultationAttachment,
			//	TmpFileName = data.tempDocName
			//});
			var content = System.Text.UTF8Encoding.UTF8.GetBytes("my data");
			var fileName = "mytestfile.txt";
			var metadata = "this is my meta data";
			this.uploader.UploadDataAsync(content, fileName, metadata);
		}

	}
}
